
local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

/* Produce the household roster dataset to do summary stats */
use "`folder'/Data/Baseline_Raw/SEC03", clear
* Create birth order variable for Child
gen elig_type = q03_302 if q03_304a <= 1

by www hh: egen elig_child = max(elig_type)

by www hh: egen newborn = max(q03_302) if elig_child == . & (q03_302 == 3 | q03_302 == 4)
by www hh: egen newborn_child = max(newborn)

replace elig_child = newborn_child if elig_child == .

drop elig_type newborn newborn_child

gen kid_type = elig_child == q03_302

gsort www hh kid_type -q03_304a -q03_304b

by www hh kid_type: gen birth_order = _n if kid_type == 1

by www hh: egen minage = min(q03_304a) if birth_order ~= .

replace birth_order = . if minage >= 13

drop elig_child kid_type minage

sort www hh q03_idc

rename q03_idc id_code
rename q03_301 name
rename q03_302 relationship
rename q03_303 sex
rename q03_304a age_years
rename q03_304b age_months
rename q03_305 literacy
rename q03_306 school
rename q03_307 highest_grade
rename q03_308 marital_status
rename q03_309a occupation_main
rename q03_309b occupation_secondary
rename q03_310 residing
rename q03_311 eligible
by www hh, sort: egen num_hh_members_raw = count(id_code)
by www hh, sort: egen num_hh_members_residing_raw = count(id_code) if residing == 1
by www hh, sort: egen num_eligible_women_raw = count(id_code) if eligible == 1 & age_year > 5
by www hh, sort: egen num_eligible_kids_raw = count(id_code) if eligible == 1 & age_year <= 5
by www hh, sort: egen num_hh_members = max(num_hh_members_raw)
by www hh, sort: egen num_hh_members_residing = max(num_hh_members_residing_raw)
by www hh, sort: egen num_eligible_women = max(num_eligible_women_raw)
by www hh, sort: egen num_eligible_kids = max(num_eligible_kids_raw)
drop *_raw
tempfile baseline_raw_fam
save "`baseline_raw_fam'", replace

/* Add on the VDC name so that we can get the treatment status */
merge m:1 www hh using "`folder'/Data/baseline_raw_hh", keepusing(vdc_name district vdc_code religion) nogen
save "`baseline_raw_fam'", replace


use "`folder'/Data/cosampling.dta", clear
keep if sampco == 1
keep vdc control_vdc info_vdc cash_vdc
by vdc, sort: keep if _n == 1
rename vdc vdc_name
merge 1:m vdc_name using "`baseline_raw_fam'", nogen
sort www hh

/* Generate one variable with all three treatment indicators, and label it,
   for the purpose of generating tables */
gen treatment = 1 if control_vdc == 1
replace treatment = 2 if info_vdc == 1
replace treatment = 3 if cash_vdc == 1

label define treatgroup 1 "Control" 2 "Info Only" 3 "Info + Cash"
label values treatment treatgroup

/* Generate the three comparison dummies so that we can do 2-way
   hypothesis testing */
gen cash_control = cash_vdc
replace cash_control = . if info_vdc == 1

gen info_control = info_vdc
replace info_control = . if cash_vdc == 1

gen cash_info = cash_vdc
replace cash_info = . if control_vdc == 1

save "`folder'/Data/baseline_raw_fam", replace
